package com.fpswork.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
import com.fpswork.common.Constant;

import java.util.Collection;
import java.util.List;


public class JSONUtils {

    private static SerializeConfig serializeConfig = new SerializeConfig();
    static {
        serializeConfig.put(java.util.Date.class, new SimpleDateFormatSerializer(Constant.FORMAT_DATETIME));
        serializeConfig.put(java.sql.Date.class, new SimpleDateFormatSerializer(Constant.FORMAT_DATETIME));
        serializeConfig.put(java.sql.Timestamp.class, new SimpleDateFormatSerializer(Constant.FORMAT_DATETIME));
    }

    public static void main(String[] args) {
    	
    }	

   public static String toJSONString(Object object){
       return  toJSONString(object,serializeConfig);
   }


    public static String toJSONString(Object object, SerializerFeature... features){
        return  toJSONString(object,serializeConfig,features);
    }


    public static String toJSONString(Object object ,SerializeConfig config, SerializerFeature... features) {
        return JSON.toJSONString(object, config, (SerializeFilter)null, features);
    }


    public static JSONObject parseObject(String text) {
        return  JSON.parseObject(text);
    }

    public static JSONObject toJSONObject(Object object) {
        return   (JSONObject) JSON.toJSON( object);
    }

    @SuppressWarnings("rawtypes")
	public static JSONArray toJSONArray(Collection object) {
        return   (JSONArray) JSON.toJSON( object);
    }

    public static  <T> T parseObject(String text, Class<T>  clazz, Feature... features) {
        return  JSON.parseObject(  text, clazz,  features);
     }

 
    public static <T> List<T> parseArray(String text, Class<T> clazz) {
        return  JSON.parseArray(  text, clazz);
    }

    public static JSONArray parseArray(String text) {
        return  JSON.parseArray(  text);
    }


    public static JSONArray parseArray(Object list) {
        return  (JSONArray)JSONArray.toJSON(list);
    }


}
